﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Diagnostics;

namespace Xceed.Silverlight.Data
{
  [DebuggerDisplay( "Value = {Value}" )]
  internal sealed class IndexKey : IComparable<IndexKey>, IComparable
  {
    #region Constructor

    internal IndexKey( object owner, DataQueryContext context )
    {
      if( owner == null )
        throw new ArgumentNullException( "owner" );

      if( context == null )
        throw new ArgumentNullException( "context" );

      this.Owner = owner;
      this.Context = context;
    }

    internal IndexKey( object owner, DataQueryContext context, long value )
      : this( owner, context )
    {
      this.Value = value;
    }

    #endregion

    #region Value Property

    internal Nullable<long> Value
    {
      get;
      set;
    }

    #endregion

    #region Context Property

    internal DataQueryContext Context
    {
      get;
      private set;
    }

    #endregion

    #region Private Owner Property

    private object Owner
    {
      get;
      set;
    }

    #endregion

    #region IComparable<IndexKey> Members

    public int CompareTo( IndexKey key )
    {
      if( ( key == null ) || ( !object.ReferenceEquals( key.Owner, this.Owner ) ) )
        throw new DataInternalException( "Cannot compare the keys that are from different data sources." );

      if( object.ReferenceEquals( key, this ) )
        return 0;

      bool reversedSortOrder;
      if( !key.Context.IsSimilarContext( this.Context, out reversedSortOrder ) )
        throw new DataInternalException( "Cannot compare the keys that have been generated in different context." );

      if( ( this.Value != null ) && ( key.Value != null ) )
      {
        long firstIndex = this.Value.Value;
        long secondIndex = key.Value.Value;

        // Convert one index to make them comparable.
        if( reversedSortOrder )
        {
          long dataSourceSize = Math.Max( this.Context.DataSourceSize, key.Context.DataSourceSize );
          secondIndex = dataSourceSize - secondIndex - 1;
        }

        return firstIndex.CompareTo( secondIndex );
      }
      else if( this.Value == null )
      {
        return -1;
      }
      else if( key.Value == null )
      {
        return 1;
      }
      else
      {
        // Both keys have an unknown index.  Since unknown indexes must not be 
        // considered equals, we'll sort them out using another criteria.
        return ( this.GetHashCode() <= key.GetHashCode() ) ? -1 : 1;
      }
    }

    #endregion

    #region IComparable Members

    public int CompareTo( object key )
    {
      return this.CompareTo( key as IndexKey );
    }

    #endregion
  }
}
